Skip to content

Parser: fix quadratic parse time on REGEXP, MATCH, GLOB and DIV chains - #3033

Open
moshap-firebolt wants to merge 1 commit into
apache:mainfrom
moshap-firebolt:infix-no-clone
Open

moshap-firebolt wants to merge 1 commit into
apache:mainfrom
moshap-firebolt:infix-no-clone

Conversation

@moshap-firebolt

Copy link
Copy Markdown
Contributor

The parse_infix hooks for REGEXP, MATCH and GLOB in SQLiteDialect and for DIV in MySqlDialect and SparkSqlDialect receive the left operand as &Expr and clone it. In a left-deep chain each operator clones everything to its left, so the parse time is quadratic in the chain length, and a long chain overflows the stack in the recursive clone:

SELECT a REGEXP b REGEXP b REGEXP b ...
SELECT a DIV b DIV b DIV b ...

Release build, main 6a37ecb3: 1,000 operators take 80 ms, 2,000 take 330 ms and 4,000 take 1.6 s. With this change 4,000 take 6 ms.

Parser::parse_infix now handles these keywords for those dialects. It owns the operand, so it moves it into the BinaryOp. The hooks and the shared parse_div_infix helper are removed. The AST is unchanged.

Tests: a 5,000-operator chain for each of the four operators, which overflows the stack without the change, and a REGEXP ANY(b) and a DIV ANY(b), whose right operand stays an ordinary expression.

This PR was prepared with AI assistance (Claude).

Dialects: SQLite, MySQL, Spark

🤖 Generated with Claude Code

The SQLite hook for REGEXP, MATCH and GLOB and the MySQL and Spark hooks
for DIV receive the left operand by reference and clone it, so a chain
of n operators takes time quadratic in n, and a long one overflows the
stack in the recursive clone. Parse them in parse_infix, which owns the
operand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.54%. Comparing base (6a37ecb) to head (ad69587).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3033      +/-   ##
==========================================
- Coverage   83.56%   83.54%   -0.02%     
==========================================
  Files          42       42              
  Lines       35689    35650      -39     
  Branches    35689    35650      -39     
==========================================
- Hits        29822    29783      -39     
  Misses       2429     2429              
  Partials     3438     3438              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the fuzzer seeds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants